/*
 * @Author: songwh
 * @Date: 2017-06-26 15:47:03
 * @Last Modified by: songwh
 * @Last Modified time: 2018-02-05 16:33:47
 */
import Vue from 'vue'
const SUCCESS = 1000000
const API_ROOT = server.root + '/newmanage'
/**
 * 首页产品展示板块列表
 */
const productListResource = Vue.resource(API_ROOT + '/product/section/list')
export const getSectionList = ({dispatch}, params) => {
  return productListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 首页产品展示板块修改
 */
const productSaveResource = Vue.resource(API_ROOT + '/product/section/save')
export const saveSectionItem = ({dispatch}, params) => {
  return productSaveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 首页产品展示版本删除
 */
const productDeleteResource = Vue.resource(API_ROOT + '/product/section/delete')
export const deleteSectionItem = ({dispatch}, params) => {
  return productDeleteResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 首页产品展示板块交换位置
 */
const productSwapItemResource = Vue.resource(API_ROOT + '/product/section/swap')
export const swapSectionItem = ({dispatch}, params) => {
  return productSwapItemResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INVITE_LIST_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 首页产品子项交换位置
 */
const swapClassfyResource = Vue.resource(API_ROOT + '/product/classify/swap')
export const swapClassfySwap = ({dispatch}, params) => {
  return swapClassfyResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INVITE_LIST_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 首页产品展示子类保存
 */
const classifySaveResource = Vue.resource(API_ROOT + '/product/classify/save')
export const saveClassifyItem = ({dispatch}, params) => {
  return classifySaveResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 首页产品展示子类删除
 */
const classifyDeleteResource = Vue.resource(API_ROOT + '/product/classify/delete')
export const deleteClassifyIdItem = ({dispatch}, params) => {
  return classifyDeleteResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 首页产品展示子项列表
 */
const itemListResource = Vue.resource(API_ROOT + '/product/item/list')
export const getItemList = ({dispatch}, params) => {
  return itemListResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 首页产品展示子项修改
 */
const itemSaveResource = Vue.resource(API_ROOT + '/product/item/save')
export const saveItem = ({dispatch}, params) => {
  return itemSaveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 首页产品展示子项删除
 */
const itemDeleteResource = Vue.resource(API_ROOT + '/product/item/delete')
export const deleteItem = ({dispatch}, params) => {
  return itemDeleteResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVITE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
export const toogleSectionListFresh = ({dispatch}) => {
  dispatch('INVITE_LIST_FRESH', 1)
}

/**
 * 首页产品子项交换位置
 */
const swapItemResource = Vue.resource(API_ROOT + '/product/item/swap')
export const swapItem = ({dispatch}, params) => {
  return swapItemResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        return dispatch('INVITE_LIST_FRESH', 1)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 投资大讲堂列表
 */
const investLectureResource = Vue.resource(API_ROOT + '/news/investlecture/list')
export const getInvestLectureList = ({dispatch}, params) => {
  return investLectureResource.get(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVEST_LECTURE_LIST_FRESH', 0)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 投资大讲堂 - 保存
 */
const investLectureSaveResource = Vue.resource(API_ROOT + '/news/investlecture/save')
export const saveinvestLecture = ({dispatch}, params) => {
  return investLectureSaveResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVEST_LECTURE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
/**
 * 投资大讲堂 - 删除
 */
const investLectureDeleteResource = Vue.resource(API_ROOT + '/news/investlecture/delete')
export const deleteinvestLecture = ({dispatch}, params) => {
  return investLectureDeleteResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVEST_LECTURE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}

/**
 * 投资大讲堂 - 置顶
 */
const investLectureTopResource = Vue.resource(API_ROOT + '/news/investlecture/top')
export const investLectureItemTop = ({dispatch}, params) => {
  return investLectureTopResource.save(params)
    .then((res) => {
      if (+res.data.code === SUCCESS) {
        dispatch('INVEST_LECTURE_LIST_FRESH', 1)
        return Promise.resolve(res.data.data)
      }
      return Promise.reject(res.data)
    })
}
